package cate.game.role.res.goldmall.po;

import cate.common.table.d.GDFunc;
import cate.common.table.d.GDMall;
import cate.common.table.d.GDObj;
import cate.common.table.res.goldmall.row.GoldMallBaseRow;
import cate.common.util.GameResult;
import cate.game.activity.base.core.RoleActivitySingle;
import cate.game.res.MixRes;
import cate.game.res.MixResAdder;
import cate.game.res.MixResConsumer;
import cate.game.role.Role;
import cate.game.role.RoleDependent;
import cate.game.role.mall.RefreshTimeCheck;
import cate.game.role.res.goldmall.msg.GoldMallDataResp;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;

import java.util.ArrayList;
import java.util.List;

@NoteClass(value = "金币商店需要持久化的数据")
public class GoldMall extends RoleDependent {
    @NoteField(value = "上次刷新时间")
    public long lastRefreshTime;

    @NoteField("宝箱数据")
    public List<GoldBox> boxList;

    @Override
    public void initialize(Role role) {
        super.initialize(role);
        if (boxList == null) {
            boxList = new ArrayList<>();
        }
    }

    @Override
    public void onEnter() {
        checkRefresh();
    }

    /** 购买金币宝箱 */
    public GameResult<Void> buy(int boxId) {
        GameResult<Void> r = new GameResult<>();
        GoldBox box = getBox(boxId);
        if (box == null) {
            return r.fail("没有这种宝箱");
        }
        GoldMallBaseRow row = box.getRow(role.getGame());
        if (row == null) {
            return r.fail("没有这种宝箱");
        }
        int limtNum = row.limitNum;
        RoleActivitySingle activitySingle = role.getActivity().getEnableActivitySingle(GDFunc.Help.GOLD_COMEON_TASK);
        if (activitySingle != null) {
            limtNum = row.limitNum + row.activityAddTimes;
        }
        if (box.num >= limtNum) {
            return r.fail("没有可用的购买次数");
        }
        return new MixResConsumer<Void>()
                .res(new MixRes(row.consumeStr))
                .tips(true)
                .success(e -> {
                    box.num++;
                    new MixResAdder()
                            .setDialogReward(true)
                            .setSendMailWhenFull(true)
                            .setRes(calReward(boxId))
                            .exe(role);
                    role.getHistory().action.goldComeOn();
                    noticeUpdate();
                    return null;
                }).exe(role);
    }

    public MixRes calReward(int boxId) {
        long m0 = role.getGame().table.goldMall.reward.getM0(role.getBase().level, boxId);
        //TODO  vip加成
        double scale = 1.0d;
        return new MixRes(GDObj.getMixResStr(GDObj.Type.NUM, GDObj.Num.M0, Math.round(m0 * scale)));
    }

    /** 判断是否要要刷新 */
    public void checkRefresh() {
        if (RefreshTimeCheck.isRefresh(role.getGame(), lastRefreshTime, GDMall.GoldenMall.GOLD_MALL_CYCLE)) {
            refreshGoldMall();
        }
    }

    /** 刷新金币商店列表 */
    private void refreshGoldMall() {
        boxList.clear();
        for (GoldMallBaseRow row : role.getGame().table.goldMall.base.getList()) {
            GoldBox po = new GoldBox(row.id);
            boxList.add(po);
        }
        lastRefreshTime = role.getGame().time.now();
    }

    /** 获取金币宝箱 */
    private GoldBox getBox(int boxId) {
        return boxList.stream().filter(e -> e.id == boxId).findAny().orElse(null);
    }

    /** 向客户端通知更新消息 */
    public void noticeUpdate() {
        checkRefresh();
        role.sendNow(new GoldMallDataResp(this));
    }
}
